package com.mhy.slecterdemo;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.mhy.picselecter.SelectorHelper;
import com.mhy.picselecter.data.ConstantData;
import com.mhy.picselecter.utils.PSGlideUtil;

import java.io.File;
import java.util.ArrayList;

public class MainActivity extends BaseActivity {
    Activity context;
    TextView result;
    ImageView imv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        result = findViewById(R.id.result);
        imv = findViewById(R.id.imv);
    }


    private ArrayList<String> selectedPictures = new ArrayList<>();

    //选图、照相 返回结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1001) {
                ArrayList<String> backPics = data.getStringArrayListExtra(ConstantData.KEY_BACK_PICTURES);
                if (backPics != null && backPics.size() > 0) {
                    selectedPictures.clear();
                    selectedPictures.addAll(backPics);
                    PSGlideUtil.loadImage(this, backPics.get(0), imv);//如果列表就for
                    StringBuilder filePath = new StringBuilder("路径:");
                    for (String s : selectedPictures) {
                        filePath.append(s).append("\n");
                    }
//                    String filePath = "路径:" + backPics.get(0);
                    result.setText(filePath.toString());
                    Log.e("路径:", filePath.toString());
                    File file = new File(filePath.toString());
                    Log.e("file路径:", file.getPath());
                }
            }
        }
    }

    //打开camrea
    public void take(View view) {
        checkPermission(new CheckPermListener() {
            @Override
            public void agreeAllPermission() {
                SelectorHelper.takePhoto(context, true, 1001);

            }
        }, "需要拍照和读取文件权限", Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    public void select(View view) {
        checkPermission(new CheckPermListener() {
            @Override
            public void agreeAllPermission() {
//        单选 withCamera false不显示相机
                SelectorHelper.selectPicture(context, true, false, 1001);

            }
        }, "需要拍照和读取文件权限", Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    public void selectMore(View view) {
        //多选 最大数量
        int maxCount = 9;
        SelectorHelper.selectPictures(context, maxCount, true, selectedPictures, 1001);
    }


    @Override
    protected void backFromSetting() {
        select(null);
    }
}
